/**
 ******************************************************************************
 * @file    zb32l03x_hal_rtc.h
 * @author  MCU Software Team
 * @Version V1.0.0
 * @Date    2022/01/19
 * @brief   Header file of RTC HAL module.
 ******************************************************************************
 **/


#ifndef __ZB32L03x_HAL_RTC_H
#define __ZB32L03x_HAL_RTC_H

#ifdef __cplusplus
extern "C" {
#endif


#include "zb32l03x_hal_def.h"

/** @addtogroup ZB32L03x_HAL_Driver
 * @{
 */

/** @addtogroup RTC
 * @{
 */



/** @defgroup RTC_Exported_Types RTC Exported Types
 * @{
 */

/**
 * @brief  RTC Time structure definition
 */
typedef struct
{
    uint8_t Hours;      /*!< Specifies the RTC Time Hour.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 23 */

    uint8_t Minutes;    /*!< Specifies the RTC Time Minutes.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

    uint8_t Seconds;    /*!< Specifies the RTC Time Seconds.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

} RTC_TimeTypeDef;

/**
 * @brief  RTC Date structure definition
 */
typedef struct
{
    uint8_t WeekDay;    /*!< Specifies the RTC Date WeekDay (not necessary for HAL_RTC_SetDate).
                            This parameter can be a value of @ref RTC_WeekDay_Definitions */

    uint8_t Month;      /*!< Specifies the RTC Date Month (in BCD format).
                            This parameter can be a value of @ref RTC_Month_Date_Definitions */

    uint8_t Date;       /*!< Specifies the RTC Date.
                            This parameter must be a number between Min_Data = 1 and Max_Data = 31 */

    uint32_t Year;      /*!< Specifies the RTC Date Year.
                            This parameter must be a number between Min_Data = 1900 and Max_Data = 2099 */

} RTC_DateTypeDef;


/**
 * @brief  RTC Alarm structure definition
 */
typedef struct
{
    uint32_t                Alarm_Enable;
    RTC_DateTypeDef         Alarm_Date;
    RTC_TimeTypeDef         Alarm_Time;     /*!< Specifies the RTC Alarm Time members */
    uint32_t                Alarm;          /*!< Specifies the alarm ID .
                                                This parameter can be a value of @ref RTC_Alarms_Definitions */
} RTC_AlarmTypeDef;

/**
 * @brief  HAL RTC State structures definition
 */
typedef enum
{
    HAL_RTC_STATE_RESET             = 0x00U,  /*!< RTC not yet initialized or disabled */
    HAL_RTC_STATE_READY             = 0x01U,  /*!< RTC initialized and ready for use   */
    HAL_RTC_STATE_BUSY              = 0x02U,  /*!< RTC process is ongoing              */
    HAL_RTC_STATE_TIMEOUT           = 0x03U,  /*!< RTC timeout state                   */
    HAL_RTC_STATE_ERROR             = 0x04U   /*!< RTC error state                     */

} HAL_RTCStateTypeDef;

/**
 * @brief  HAL RTC alarm2 structures definition
 */
typedef enum
{
    HAL_RTC_ALARM2_CLOSE             = 0x00U,   /*!< RTC ALARM2 disabled                   */
    HAL_RTC_ALARM2_1S,                          /*!< RTC ALARM2 cycle time set 1s          */
    HAL_RTC_ALARM2_1P2S,                        /*!< RTC ALARM2 cycle time set 1/2s        */
    HAL_RTC_ALARM2_1P4S,                        /*!< RTC ALARM2 cycle time set 1/4s        */
    HAL_RTC_ALARM2_1P8S,                        /*!< RTC ALARM2 cycle time set 1/8s        */
    HAL_RTC_ALARM2_1P16S,                       /*!< RTC ALARM2 cycle time set 1/16s       */
    HAL_RTC_ALARM2_1P32S,                       /*!< RTC ALARM2 cycle time set 1/32s       */
    HAL_RTC_ALARM2_1P64S,                       /*!< RTC ALARM2 cycle time set 1/64s       */
    HAL_RTC_ALARM2_1P128S,                      /*!< RTC ALARM2 cycle time set 1/128s      */
    HAL_RTC_ALARM2_10S,                         /*!< RTC ALARM2 cycle time set 108s        */
    HAL_RTC_ALARM2_30S,                         /*!< RTC ALARM2 cycle time set 30s         */
    HAL_RTC_ALARM2_1M,                          /*!< RTC ALARM2 cycle time set 1 MINUTS    */
    HAL_RTC_ALARM2_30M,                         /*!< RTC ALARM2 cycle time set 30 MINUTS   */
    HAL_RTC_ALARM2_60M,                         /*!< RTC ALARM2 cycle time set 60 MINUTS   */
    HAL_RTC_ALARM2_12H,                         /*!< RTC ALARM2 cycle time set 12 HOURS    */
    HAL_RTC_ALARM2_24H                          /*!< RTC ALARM2 cycle time set 24 HOURS    */

} HAL_RTCAlarm2TypeDef;

/**
 * @brief  RTC Init Configuration Structure definition
 */
typedef struct
{
    uint32_t ClockSource;       /*!< Specifies the RTC clock source
                                    This parameter can be a value of @ref RTC_CLOCK_SOURCE_Definitions*/

    uint32_t HourFormat;        /*!< Specifies the RTC Hour Formate
                                    This parameter can be a value of @ref RTC_Hour_Formats_Definition*/

    uint32_t TimeAdjustMode;    /*!< Specifies the RTC clock source
                                    This parameter can be a value of @ref RTC_TIME_ADJUST_MODE_Definitions*/

    int8_t TimeTrim;            /*!< Specifies the RTC clock trimming
                                    This parameter must be a number between Min_Data = -128 and Max_Data = 127 */

    uint16_t HxtDivider;        /*!< Specifies the HXT clock divider
                                    This parameter must be a number between Min_Data = 0 and Max_Data = 0x3ff */

} RTC_InitTypeDef;


/**
 * @brief  Time Handle Structure definition
 */
typedef struct
{
    RTC_TypeDef                 *Instance;          /*!< Register base address    */

    RTC_InitTypeDef             Init;               /*!< RTC required parameters  */

    RTC_DateTypeDef             DateToUpdate;       /*!< Current date set by user and updated automatically  */

    HAL_LockTypeDef             Lock;               /*!< RTC locking object       */

    __IO HAL_RTCStateTypeDef    State;              /*!< Time communication state */

} RTC_HandleTypeDef;

/**
 * @}
 */


/** @defgroup RTC_Exported_Constants RTC Exported Constants
 * @{
 */

/** @defgroup RTC_WRITE_PROTECT_KEY  write KEY before write to register
 * @{
 */
#define RTC_WRITE_PROTECT_KEY1              0xCAU
#define RTC_WRITE_PROTECT_KEY2              0x53U

/**
 * @}
 */

/** @defgroup RTC_Hour_Formats_Definitions RTC Hour Formats
 * @{
 */
#define RTC_HOURFORMAT_24                   RTC_CR_FMT
#define RTC_HOURFORMAT_12                   0x00000000U
/**
 * @}
 */

/** @defgroup RTC_AM_PM_Definitions RTC AM PM Definitions
 * @{
 */

#define RTC_HOURFORMAT12_AM                 ((uint8_t)0x00U)
#define RTC_HOURFORMAT12_PM                 ((uint8_t)0x40U)
/**
 * @}
 */

/** @defgroup RTC_TIME_ADJUST_MODE_Definitions RTC time adjust mode Definitions
 * @{
 */
#define RTC_TIME_ADJUST_SEC60               (0x0000U)
#define RTC_TIME_ADJUST_SEC30               (0x0100U)
#define RTC_TIME_ADJUST_SEC15               (0x0200U)
#define RTC_TIME_ADJUST_SEC06               (0x0300U)

/**
 * @}
 */

/** @defgroup RTC_CLOCK_SOURCE_Definitions RTC CLOCK Source Definitions
 * @{
 */
#define RTC_CLOCK_LXT                       (0x00UL<<RTC_CLKCR_RTCCKSEL_Pos)
#define RTC_CLOCK_SIRC                      (0x01UL<<RTC_CLKCR_RTCCKSEL_Pos)
#define RTC_CLOCK_HXT                       (0x02UL<<RTC_CLKCR_RTCCKSEL_Pos)

/**
 * @}
 */
#define RTC_BYPASS_HAD_MAX                  (0x08U)

/** @defgroup RTC_Automatic_Prediv_1_Second Automatic calculation of prediv for 1sec timebase
 * @{
 */
#define RTC_AUTO_1_SECOND                   0xFFFFFFFFU

/**
 * @}
 */
#define RTC_BCD_TO_BYTE_MASK                 0xFFU

/** @defgroup RTC_Month_Date_Definitions Month Definitions
 * @{
 */

/* Coded in BCD format */
#define RTC_MONTH_JANUARY                   ((uint8_t)0x01)
#define RTC_MONTH_FEBRUARY                  ((uint8_t)0x02)
#define RTC_MONTH_MARCH                     ((uint8_t)0x03)
#define RTC_MONTH_APRIL                     ((uint8_t)0x04)
#define RTC_MONTH_MAY                       ((uint8_t)0x05)
#define RTC_MONTH_JUNE                      ((uint8_t)0x06)
#define RTC_MONTH_JULY                      ((uint8_t)0x07)
#define RTC_MONTH_AUGUST                    ((uint8_t)0x08)
#define RTC_MONTH_SEPTEMBER                 ((uint8_t)0x09)
#define RTC_MONTH_OCTOBER                   ((uint8_t)0x10)
#define RTC_MONTH_NOVEMBER                  ((uint8_t)0x11)
#define RTC_MONTH_DECEMBER                  ((uint8_t)0x12)

/**
 * @}
 */

/** @defgroup RTC_WeekDay_Definitions WeekDay Definitions
 * @{
 */
#define RTC_WEEKDAY_MONDAY                  ((uint8_t)0x01)
#define RTC_WEEKDAY_TUESDAY                 ((uint8_t)0x02)
#define RTC_WEEKDAY_WEDNESDAY               ((uint8_t)0x03)
#define RTC_WEEKDAY_THURSDAY                ((uint8_t)0x04)
#define RTC_WEEKDAY_FRIDAY                  ((uint8_t)0x05)
#define RTC_WEEKDAY_SATURDAY                ((uint8_t)0x06)
#define RTC_WEEKDAY_SUNDAY                  ((uint8_t)0x00)

/**
 * @}
 */

/** @defgroup RTC_AlarmEnable_Definitions  alarm enable Definitions
 * @{
 */
#define RTC_ALARM_DISABLE                   ((uint8_t)0x00)
#define RTC_ALARM_YEAR_ENABLE               RTC_ALM1DATE_ALMYEAREN
#define RTC_ALARM_MONTH_ENABLE              RTC_ALM1DATE_ALMMONEN
#define RTC_ALARM_DATE_ENABLE               RTC_ALM1DATE_ALMDAYEN
#define RTC_ALARM_WEEK_ENABLE               RTC_ALM1DATE_ALMWEEKEN
#define RTC_ALARM_HOURS_ENABLE              RTC_ALM1DATE_ALMHOUREN
#define RTC_ALARM_MINUTES_ENABLE            RTC_ALM1DATE_ALMMINEN
#define RTC_ALARM_SECONDS_ENABLE            RTC_ALM1DATE_ALMSECEN

#define RTC_ALARM_ALL_ENABLE                (RTC_ALM1DATE_ALMYEAREN | RTC_ALM1DATE_ALMMONEN | RTC_ALM1DATE_ALMDAYEN | \
                                             RTC_ALM1DATE_ALMWEEKEN |                                                 \
                                             RTC_ALM1DATE_ALMHOUREN | RTC_ALM1DATE_ALMMINEN | RTC_ALM1DATE_ALMSECEN)

#define RTC_ALARM_ENABLE                    RTC_CR_ALM1EN


/**
 * @}
 */

/** @defgroup RTC_Alarms_Definitions Alarms Definitions
 * @{
 */
#define RTC_ALARM_1                        0x00U
#define RTC_ALARM_2                        0x01U

/**
 * @}
 */

/**
 * @}
 */


/** @defgroup RTC_Exported_macros RTC Exported Macros
 * @{
 */

/**
 * @brief Enter RTC  read/write mode .
 * @retval None.
 */

#define __HAL_RTC_ENTER_RW(__HANDLE__)                              \
            do{                                                     \
                RTC_Disable_Write_Protected(__HANDLE__);            \
                SET_BIT((__HANDLE__)->Instance->ISR, RTC_ISR_WAIT); \
            } while(0U)

/**
 * @brief Exit RTC  read/write mode.
 * @retval None.
 */
#define __HAL_RTC_EXIT_RW(__HANDLE__)                                 \
            do{                                                       \
                RTC_Disable_Write_Protected(__HANDLE__);              \
                CLEAR_BIT((__HANDLE__)->Instance->ISR, RTC_ISR_WAIT); \
            } while(0U)

/**
 * @brief Get WAITF Status .
 * @retval None.
 */
#define __HAL_RTC_GET_WAITF_STATUS(__HANDLE__)          ((READ_BIT((__HANDLE__)->Instance->ISR, RTC_ISR_WAITF) == RTC_ISR_WAITF) ? SET : RESET)


/**
 * @brief Enable RTC .
 * @retval None.
 */
#define __HAL_RTC_ENABLE(__HANDLE__)                               \
            do{                                                    \
                RTC_Disable_Write_Protected(__HANDLE__);           \
                SET_BIT((__HANDLE__)->Instance->CR, RTC_CR_START); \
            } while(0U)

/**
 * @brief Disable RTC .
 * @retval None.
 */
#define __HAL_RTC_DISABLE(__HANDLE__)                                \
            do{                                                      \
                RTC_Disable_Write_Protected(__HANDLE__);             \
                CLEAR_BIT((__HANDLE__)->Instance->CR, RTC_CR_START); \
            } while(0U)

/**
  * @brief Enable RTC Alm1.
  * @retval None.
  */
#define __HAL_RTC_ENABLE_ALM1(__HANDLE__)                            \
            do{                                                      \
                RTC_Disable_Write_Protected(__HANDLE__);             \
                SET_BIT((__HANDLE__)->Instance->CR, RTC_CR_ALM1EN);  \
            } while(0U)                                              \

/**
  * @brief Disable RTC ALM1.
  * @retval None.
  */
#define __HAL_RTC_DISABLE_ALM1(__HANDLE__)                            \
            do{                                                       \
                RTC_Disable_Write_Protected(__HANDLE__);              \
                CLEAR_BIT((__HANDLE__)->Instance->CR, RTC_CR_ALM1EN); \
            } while(0U)

/**
 * @brief Enable RTC Clock.
 * @retval None.
 */
#define __HAL_RTC_ENABLE_CLOCK(__HANDLE__)                                 \
            do{                                                            \
                RTC_Disable_Write_Protected(__HANDLE__);                   \
                SET_BIT((__HANDLE__)->Instance->CLKCR, RTC_CLKCR_RTCCKEN); \
            } while(0U)


/**
 * @brief Disable RTC Clock.
 * @retval None.
 */
#define __HAL_RTC_DISABLE_CLOCK(__HANDLE__)                              \
            do{                                                          \
            RTC_Disable_Write_Protected(__HANDLE__);                     \
            CLEAR_BIT((__HANDLE__)->Instance->CLKCR, RTC_CLKCR_RTCCKEN); \
            } while(0U)

/**
 * @}
 */


/** @addtogroup RTC_Private_Macros
 * @{
 */

#define IS_RTC_ASYNCH_PREDIV(PREDIV)        (((PREDIV) <= 0xFFFFFU) || ((PREDIV) == RTC_AUTO_1_SECOND))
#define IS_RTC_HOUR24(HOUR)                 ((HOUR) <= 23U)
#define IS_RTC_MINUTES(MINUTES)             ((MINUTES) <= 59U)
#define IS_RTC_SECONDS(SECONDS)             ((SECONDS) <= 59U)
#define IS_RTC_FORMAT(FORMAT)               (((FORMAT) == RTC_FORMAT_BIN) || ((FORMAT) == RTC_FORMAT_BCD))
#define IS_RTC_YEAR(YEAR)                   ((YEAR) >= 1900U && (YEAR) <= 2099U)
#define IS_RTC_MONTH(MONTH)                 ((MONTH) >= 1U && (MONTH) <= 12U)
#define IS_RTC_DATE(DATE)                   ((DATE) >= 1U && (DATE) <= 31U)
#define IS_RTC_WEEKDAY(WEEKDAY)             (((int)(WEEKDAY) >= (int)RTC_WEEKDAY_SUNDAY) && ((WEEKDAY) <= RTC_WEEKDAY_SATURDAY))
#define IS_RTC_ALARM(ALARM)                 ((ALARM) == RTC_ALARM_1 || (ALARM) == RTC_ALARM_2)

#define IS_RTC_ALL_INSTANCE(INSTANCE)       (INSTANCE == RTC)
#define IS_RTC_CLOCK_SOURCE(SOURCE)         ((SOURCE == RTC_CLOCK_LXT) || (SOURCE == RTC_CLOCK_SIRC) || (SOURCE == RTC_CLOCK_HXT))

#define IS_RTC_HOUR_FORMAT(FORMAT)          ((FORMAT == RTC_HOURFORMAT_12) || (FORMAT == RTC_HOURFORMAT_24))

#define IS_RTC_TIME_ADJUST_MODE(MODE)       ((MODE) == RTC_TIME_ADJUST_SEC60 || (MODE) == RTC_TIME_ADJUST_SEC30 || \
                                             (MODE) == RTC_TIME_ADJUST_SEC15 || (MODE) == RTC_TIME_ADJUST_SEC06)

#define IS_RTC_TIME_TRIM(TRIM)              ((TRIM > (int8_t)(-128)) && (TRIM < (int8_t)(127)))
#define IS_RTC_HXT_DIVIDER(DIV)             ((DIV > (uint16_t)(0)) && (TRIM < (uint16_t)(0x3ff)))
/**
 * @}
 */

/** @addtogroup RTC_Exported_Functions
 * @{
 */


/* Initialization and de-initialization functions  ****************************/
/** @addtogroup RTC_Exported_Functions_Group1
 * @{
 */
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc);
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc);

void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc);
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc);
/**
 * @}
 */

/* RTC Time and Date functions ************************************************/
/** @addtogroup RTC_Exported_Functions_Group2
 * @{
 */
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate);
HAL_StatusTypeDef HAL_RTC_SetTime_SetDate(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Hour_Format, RTC_DateTypeDef *sDate);
HAL_StatusTypeDef HAL_RTC_GetTime_Date(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, RTC_DateTypeDef *sDate);


/**
 * @}
 */

/* RTC Alarm functions ********************************************************/
/** @addtogroup RTC_Exported_Functions_Group3
 * @{
 */
HAL_StatusTypeDef HAL_RTC_Alarm1_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime);
HAL_StatusTypeDef HAL_RTC_Alarm1_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate);
HAL_StatusTypeDef HAL_RTC_Alarm1_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime);
HAL_StatusTypeDef HAL_RTC_Alarm1_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate);

HAL_StatusTypeDef HAL_RTC_Alarm1_Set_INT_Source(RTC_HandleTypeDef *hrtc, uint32_t AlarmEn);
HAL_StatusTypeDef HAL_RTC_Alarm_INT_Config(RTC_HandleTypeDef *hrtc, uint32_t Alarm, FunctionalState NewState);
HAL_StatusTypeDef HAL_RTC_Alarm1_Config(RTC_HandleTypeDef *hrtc, FunctionalState NewState);
HAL_StatusTypeDef HAL_RTC_Alarm2_Config(RTC_HandleTypeDef *hrtc, FunctionalState NewState, HAL_RTCAlarm2TypeDef CycleTime);

FlagStatus HAL_RTC_Alarm_Get_Flag(uint32_t Alarm);
void HAL_RTC_Alarm_Clear_Flag(uint32_t Alarm);
void HAL_RTC_IRQHandler(void);

/**
 * @}
 */

/* Peripheral State functions *************************************************/
/** @addtogroup RTC_Exported_Functions_Group4
 * @{
 */
HAL_StatusTypeDef HAL_RTC_1HZ_Config(RTC_HandleTypeDef *hrtc, FunctionalState NewState);
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc);
/**
 * @}
 */


/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* __ZB32L03x_HAL_RTC_H */
